﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using PerCiGal.Types;

namespace PerCiGal.BPC
{
	public class BPCCollection
	{
		/// <summary>
		/// Create a new collection
		/// </summary>
		/// <param name="collectionInfo"></param>
		public void Create(Types.COLLECTION_INFO collectionInfo)
		{
			try
			{
				/* check if exists */
				if (File.Exists(collectionInfo.FILE_PATH))
					File.Delete(collectionInfo.FILE_PATH);

				var dalCollection = new DAL.DALCollection();

				// Create file, table e insert default value for genres, nations, subtitle_languages
				dalCollection.Create(collectionInfo);

				// Insert collecion info data
				dalCollection.Insert(collectionInfo);
			}
			catch (Exception exc)
			{
				throw exc;
			}
		}


		public Types.COLLECTION Open(string filePath, string password)
		{
			try
			{
				var collection = new COLLECTION();

				var dalCollection = new DAL.DALCollection();

				collection.CollectionInfo = dalCollection.CollectionInfo(filePath, password);

				collection.Genres = dalCollection.Genres(collection.CollectionInfo);

				collection.Nations = dalCollection.Nations(collection.CollectionInfo);

				collection.SubtitleLanguages = dalCollection.SubtitleLanguages(collection.CollectionInfo);

				var dalMovie = new DAL.DALMovie(collection.CollectionInfo);

				collection.MovieList = dalMovie.MovieListSelectAll();

				return collection;
			}
			catch (Exception exc)
			{
				throw exc;
			}
		}
	}
}
